<?php
/**
 * Created by PhpStorm.
 * User: 王柯 <wangke.email@foxmail.com>
 * Date: 2018\4\20 0020
 * Time: 9:22
 */
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Log;

/**
 * 课程预算
 * Class Budget
 * @package app\admin\controller
 */
class Budget extends Controller{
    /**
     * 预算管理
     * @author 王柯 <wangke.email@foxmail.com>
     * @return void
     */
    public function index(){

    }

    /**
     * 结算预算
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function settlementBudget(){
        try {

            $data = $_POST;

            //每次课程费用
            $once_lesson_cost = $data['lesson_cost'] / $data['lesson_num'];

            //课程预算人数
            $lesson_budget_number = systemConfig('lesson_budget_number');

            //预算数据
            $budget = [];

            //课程预算人数
            $budget['lesson_budget_number'] = $lesson_budget_number;

            //预算总费用
            $budget['budget_total_cost'] = $once_lesson_cost * $lesson_budget_number;

            //推荐学生奖金
            $budget['rec_student_bonus'] = $lesson_budget_number * $data['rec_student_cost'];

            //推荐教师奖金
            $budget['rec_teacher_bonus'] = $lesson_budget_number * $data['rec_teacher_cost'];

            //推荐教室奖金
            $budget['rec_classroom_bonus'] = $lesson_budget_number * $data['rec_classroom_cost'];

            //重上保证金
            if(!isset($data['reclass_cost'])){
                $budget['reclass_bonus'] = 0;
            }else{
                $budget['reclass_bonus'] = $data['reclass_cost'];
            }

            /**
             * 管理费用以及专项费用
             */

            //管理总费用用户奖金
            $budget['total_rec_bonus'] = $lesson_budget_number * $data['total_rec_cost'];

            //省级管理费用户奖金
            $budget['province_rec_bonus'] = $lesson_budget_number * $data['province_rec_cost'];

            //市级管理费用户奖金
            $budget['city_rec_bonus'] = $lesson_budget_number * $data['city_rec_cost'];

            //区级管理费用户奖金
            $budget['area_rec_bonus'] = $lesson_budget_number * $data['area_rec_cost'];

            //校区管理费用户奖金
            $budget['school_rec_bonus'] = $lesson_budget_number * $data['school_rec_cost'];

            //专项费用用户1奖金
            $budget['spe_user1_bonus'] = $lesson_budget_number * $data['special_user1_cost'];

            //专项费用用户2奖金
            $budget['spe_user2_bonus'] = $lesson_budget_number * $data['special_user2_cost'];

            //专项费用用户3奖金
            $budget['spe_user3_bonus'] = $lesson_budget_number * $data['special_user3_cost'];

            //专项费用用户4奖金
            $budget['spe_user4_bonus'] = $lesson_budget_number * $data['special_user4_cost'];

            //专项费用用户5奖金
            $budget['spe_user5_bonus'] = $lesson_budget_number * $data['special_user5_cost'];

            //合作受益者奖金
            $budget['cooperate_bonus'] = $lesson_budget_number * $data['cooperate_cost'];

            //课堂材料费用
            $budget['material_user_bonus'] = $lesson_budget_number * $data['material_cost'];

            //教务费用判断
            if ($data['is_education_cost'] == 0) {
                $budget['education_user_bonus'] = 0;
            } else if ($data['is_education_cost'] == 1) {
                //下发教务奖金判断
                if ($data['is_education_cost'] == 0) {
                    $budget['education_user_bonus'] = 0;
                } else if ($data['is_education_cost'] == 1) {
                    //判断教务奖金类型1按班，2按人头，3按保底加人头
                    if ($data['education_cost_type'] == 1) {
                        $budget['education_user_bonus'] = $data['education_cost_class'];
                    } else if ($data['education_cost_type'] == 2) {
                        $budget['education_user_bonus'] = $lesson_budget_number * $data['education_cost_person'];
                    } else if ($data['education_cost_type'] == 3) {
                        $budget['education_user_bonus'] = $lesson_budget_number * $data['education_cost_person'] > $data['education_bdcost'] ? $lesson_budget_number * $data['education_cost_person'] : $data['education_bdcost'];
                    }
                }
            }

            //教师奖金判断
            if ($data['is_teacher_cost'] == 0) {
                $budget['teacher_bonus'] = 0;
            } else if ($data['is_teacher_cost'] == 1) {
                //下发教师奖金判断
                if ($data['is_teacher_cost'] == 0) {
                    $budget['teacher_bonus'] = 0;
                } else if ($data['is_teacher_cost'] == 1) {
                    //判断教师奖金类型1按班，2按人头，3按保底加人头
                    if ($data['teacher_cost_type'] == 1) {
                        $budget['teacher_bonus'] = $data['teacher_cost_class'];
                    } else if ($data['teacher_cost_type'] == 2) {
                        $budget['teacher_bonus'] = $lesson_budget_number * $data['teacher_cost_person'];
                    } else if ($data['teacher_cost_type'] == 3) {
                        $budget['teacher_bonus'] = $lesson_budget_number * $data['teacher_cost_person'] > $data['teacher_bdcost'] ? $lesson_budget_number * $data['teacher_cost_person'] : $data['teacher_bdcost'];
                    }
                }
            }

            //判断教室拥有者奖金
            if ($data['is_classroom_cost'] == 0) {
                $budget['classroom_owner_bonus'] = 0;
            } else if ($data['is_classroom_cost'] == 1) {
                //判断拥有者教室奖金
                if ($data['is_classroom_cost'] == 0) {
                    $budget['classroom_owner_bonus'] = 0;
                } elseif ($data['is_classroom_cost'] == 1) {
                    if ($data['classroom_cost_type'] == 1) {
                        $budget['classroom_owner_bonus'] = $data['classroom_cost_class'];
                    } elseif ($data['classroom_cost_type'] == 2) {
                        $budget['classroom_owner_bonus'] = $lesson_budget_number * $data['classroom_cost_person'];
                    } elseif ($data['classroom_cost_type'] == 3) {
                        $budget['classroom_owner_bonus'] = $lesson_budget_number * $data['classroom_cost_person'] > $data['classroom_bdcost'] ? $lesson_budget_number * $data['classroom_cost_person'] : $data['classroom_bdcost'];
                    }
                }
            }

            //预算总费用
            $total_budget = $budget['budget_total_cost'];

            unset($budget['lesson_budget_number']);

            unset($budget['budget_total_cost']);

            $array = [];
            $num = '';

            foreach ($budget as $key => $val){
                $num += $val;
                $array['total'] = $num;
                $array['limit'] = $total_budget - $array['total'];
                $array['budget_total_cost'] = $total_budget;
                $array['lesson_budget_number'] = $lesson_budget_number;
                $array[$key] = $val;
                if($array['limit'] == 0){
                    $array['result'] = '合格';
                }else{
                    $array['result'] = '不合格';
                }
            }

            if($array['limit'] == 0){
                return show(config('code.success'),'合格!',$array);
            }else{
                return show(config('code.error'),'不合格!',$array);
            }

        }catch(\Exception $e){
            Log::write($e->getMessage());
            return show(config('code.error'), $e->getMessage(), [], 500);
        }
    }

}